// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

import plugin from './components/'

import {localData} from "./assets/js/local"

// 引入element-ui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import axios from 'axios';
import Qs from 'qs';


//echart
import echarts from 'echarts'

Vue.prototype.$echart = echarts;


import { filterTime } from './assets/js/time'

axios.defaults.withCredentials = true;

Vue.use(ElementUI);
Vue.use(plugin);





//localStorage操作
Vue.prototype.localData = localData;


import store from './store/index.js';



//初租后台管理系统--测试
// Vue.prototype.host = 'http://192.168.3.55:10002';

//爱收。。。后台管理系统--正式
Vue.prototype.host =  'http://cuishoumanage.chuzumall.com:30000';

//收啊催收后台管理系统--正式
// Vue.prototype.host =  'http://118.31.57.101:30000';



//封装请求
Vue.prototype.ajax = function(options){
  let that = this;
  let data = Qs.stringify(options.data);
  axios({
    url: that.host + options.url,
    method: 'POST',
    headers: options.headers,
    data: data,
  }).then(function(res){
    if(res.status == 200 ){
      options.success(res.data);
      let code = res.data.code;
      if( code != '0'){
        that.$message.error(res.data.message);
        if(code == '1003'){
          that.logout()
        }
      }
    }
  }).catch(function(error){
    if( error.response ){
      let code = error.response.status;
      if (code == '404') {
        that.$message.error('接口不存在');
        options.success({})
      } else {
        that.$message.error('服务器错误');
        options.success({})
      }
    }
  })
}

//封装ajax
Vue.prototype.ajaxJson = function(options){
  let that = this;

  if(options.data){
      options.data.appId = that.$store.state.id
  }

  axios({
    url: that.host + options.url,
    method: 'POST',
    headers: options.headers,
    data: options.data,
  }).then(function(res){
    if(res.status == 200 ){
      options.success(res.data);
      let code = res.data.code;
      if( code != '0'){
        that.$message.error(res.data.message);
        if(code == '1003'){
          that.logout()
        }
      }
    }
  }).catch(function(error){
    if( error.response ){
      let code = error.response.status;
      if (code == '404') {
        that.$message.error('接口不存在');
        options.success({})
      } else {
        that.$message.error('服务器错误');
        options.success({})
      }
    }
  })
}

//导出
Vue.prototype.downloadExcel = function (options) {
  let that = this;
  let downObj = {}


  for (let val in options.obj) {
    if (val != 'page' && val != 'size') {
      downObj[val] = options.obj[val]
    }
  }

  that.ajaxJson({
    url: options.url,
    data: downObj,
    method: 'GET',
    success: function (data) {
      if (data.code == '0') {

        let href = that.host + data.data
        let elink = document.createElement('a') // 创建a标签
        elink.download = data.data// 文件名
        elink.style.display = 'none'
        elink.href = href
        document.body.appendChild(elink)
        elink.click() // 触发点击a标签事件
        document.body.removeChild(elink)
      } else {
        that.$message.error(data.message);
      }
    }
  })
}

Vue.prototype.downloadExcelGet = function (options) {
  let that = this;
  let downObj = {}
  that.ajaxJson({
    url: options.url,
    data: downObj,
    method: 'GET',
    success: function (data) {
      if (data.code == '0') {
        let href = that.host + data.data
        let elink = document.createElement('a') // 创建a标签
        elink.download = data.data// 文件名
        elink.style.display = 'none'
        elink.href = href
        document.body.appendChild(elink)
        elink.click() // 触发点击a标签事件
        document.body.removeChild(elink)
      } else {
        that.$message.error(data.message);
      }
    }
  })
}




//退出请求
Vue.prototype.logout = function () {
  let that = this;
  that.ajax({
    url: '/auth/logout',
    success: function (data) {
      that.localData('clean', 'login_user');
      that.$router.push({name: 'login'});
    }
  })
}



//报错信息
Vue.prototype.showMsg = function (val, status) {
  let that = this;
  that.$message({
    message: val,
    type: status
  });
}


//判断手机号
Vue.prototype.isMobile = function(val) {
  let myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  return myreg.test(val)
}

//表格显示时间
Vue.prototype.formatter_time = function (row, column, cellValue, index) {
  return cellValue ? (new Date(cellValue)).Format('yyyy-MM-dd') : ''
}
Vue.prototype.formatter_timeYMDHMS = function (row, column, cellValue, index) {
  return cellValue ? (new Date(cellValue)).Format('yyyy-MM-dd hh:mm:ss') : ''
}
Vue.prototype.formatter_timeYMDHM = function (row, column, cellValue, index) {
  return cellValue ? (new Date(cellValue)).Format('yyyy-MM-dd hh:mm') : ''
}

//表格显示本人是否接通
Vue.prototype.formatter_iscall = function (row, column, cellValue, index) {
  switch(cellValue){
    case 0:
      return "否";
    case 1:
      return "是";
    default:
      return "否"
  }
}

//表格显示本人还款意愿
Vue.prototype.formatter_repayWish = function (row, column, cellValue, index) {
  switch(cellValue){
    case 0:
      return "无";
    case 1:
      return "有";
    default:
      return "--"
  }
}

//表格显示是否愿意转告
Vue.prototype.formatter_isWishRemind = function (row, column, cellValue, index) {
  switch(cellValue){
    case 0:
      return "否";
    case 1:
      return "是";
    default:
      return "--"
  }
}


//表格显示产品
Vue.prototype.formatter_product = function (row, column, cellValue, index) {
    switch(cellValue){
      case 1:
        return "初租";
      case 2:
        return "收啊";
      case 3:
        return "租啊";
      case 4:
        return "爱收";
      case 101:
        return "信义贷";
      case 102:
        return "帮帮租";
    }
}



//其他位置时间显示
Vue.filter('filterTime', filterTime)
//时间选择器限制当前时间
Vue.prototype.pickerOptions = {
  disabledDate(time) {
    return time.getTime() > Date.now();
  }
}



// oss参数测试
Vue.prototype.OssParameter = {
    accessKeyId: 'LTAI83HVuUfaB4UI',
    accessKeySecret: 'Z7ucFCs4Z4oTjuHrEbbWex1Jj8yyrY',
    region: 'oss-cn-hangzhou',
    bucket: 'guoyuechuzu',
    folder: 'product_picture/'
};

Vue.prototype.uploadFile = function (options) {
  let that = this;
  if(!options.file){
    return;
  }
  let fileName = options.file.name;
  let filelastName = fileName.substring(fileName.lastIndexOf(".") + 1)
  let ossParameter = that.OssParameter;
  let client = new OSS.Wrapper({
    secure: true,
    accessKeyId: ossParameter.accessKeyId,
    accessKeySecret: ossParameter.accessKeySecret,
    region: ossParameter.region,
    bucket: ossParameter.bucket,
  });
  let path = ossParameter.folder  + (new Date()).getTime() + parseInt((Math.random()*100000 + 0),10) + '.'+ filelastName;
  client.multipartUpload(path, options.file).then(function (res) {
    let href = 'https://' + ossParameter.bucket + '.' + ossParameter.region + '.aliyuncs.com/' + res.name;
    options.success(href)
  }).catch(function (err) {
  });
}










Date.prototype.Format = function (fmt) {
  let o = {
    "M+": this.getMonth() + 1,         // 月份
    "d+": this.getDate(),          // 日
    "h+": this.getHours(),          // 小时
    "m+": this.getMinutes(),         // 分
    "s+": this.getSeconds(),         // 秒
    "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
    "S": this.getMilliseconds()       // 毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
};




Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,          //把store挂载到vue原型上
  router,
  components: { App },
  template: '<App/>'
})
